<?php
/**
 * Message.php
 *
 * PHP Version 5.6
 *
 * @category PHP
 * @package  Object
 * @author   ctwj <908504609@qq.com>
 * @license  MIT https://mit-license.org/
 * @link     MIT https://mit-license.org/
 * @date     2018-05-13 22:56:47
 * @modifyby ctwj
 */

namespace Telegram\Object;

use Telegram\Exception\AttributeException;

/**
 * Message class
 *
 * 消息
 *
 * @category PHP
 * @package  Object
 * @author   ctwj <908504609@qq.com>
 * @license  MIT https://mit-license.org/
 * @link     MIT https://mit-license.org/
 */
class Destination
{
    /**
     * 和resource表里面字段对应
     *
     * @var [type]
     */
    private $_id;
    private $_name;
    private $_type;
    private $_dest;
    private $_extinfo;
    private $_post_times;
    private $_status;
    private $_ins_time;

    /**
     * 初始化值
     *
     * @param [type] $data resource数据
     *
     * @return void
     */
    public function __construct($data)
    {
        foreach ($data as $k => $v) {
            $k = '_' . $k;
            $this->$k = $v;
        }
    }

    /**
     * 获取可编辑字段
     *
     * @return array
     */
    public function getKeys()
    {
        return ['name','type','dest', 'extinfo', 'post_times', 'status'];
    }

    /**
     * 设置属性的值
     *
     * @param [string] $key   key
     * @param [string] $value value
     *
     * @return void
     */
    public function __set($key, $value)
    {
        if (in_array($key, $this->getKeys())) {
            $key = '_' . $key;
            $this->$key = $value;
        }
    }

    /**
     * 获取属性
     *
     * @param [string] $key key
     *
     * @return mixed
     */
    public function __get($key)
    {
        $key = '_' . $key;
        if (property_exists($this, $key)) {
            return $this->$key;
        }
        getService('logger')->error('Attribute Error', ['destination' => $key]);
        throw new AttributeException('destination', $key);
    }

    /**
     * 获取数组
     *
     * @return array
     */
    public function getArray()
    {
        $keys = [
            'id', 'name','type','dest', 'extinfo',
            'post_times', 'status', 'ins_time'
        ];
        $result = [];
        foreach ($keys as $key) {
            $k = '_' . $key;
            $result[$key] = $this->$k;
        }
        return $result;
    }
}
